﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.WindowsAzure.ServiceRuntime;

using Domain;

namespace Repositories
{
	public class ForumRepository
	{
		string connStr;


		public ForumRepository()
		{
			//connStr = //System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString;
			connStr = RoleEnvironment.GetConfigurationSettingValue("ConnectionString");
		}



		public void InsertOrUpdateForum(Int64? subforumID, string name, string description)
		{
			SqlConnection conn = new SqlConnection(connStr);
			SqlCommand cmd = new SqlCommand();
			cmd.Connection = conn;
			cmd.CommandText = "InsertOrUpdateSubforum";
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("subforumID", subforumID));
			cmd.Parameters.Add(new SqlParameter("name", name));
			cmd.Parameters.Add(new SqlParameter("description", description));

			conn.Open();

			try
			{
				cmd.ExecuteNonQuery();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception e)
			{
				conn.Close();

				throw e;
			}
		}

		public void DeleteSubforum(Int64 subforumID)
		{
			SqlConnection conn = new SqlConnection(connStr);
			SqlCommand cmd = new SqlCommand();
			cmd.Connection = conn;
			cmd.CommandText = "DeleteSubforum";
			cmd.CommandType = CommandType.StoredProcedure;

			cmd.Parameters.Add(new SqlParameter("subforumID", subforumID));

			conn.Open();

			try
			{
				cmd.ExecuteNonQuery();
				conn.Close();
				conn.Dispose();
			}
			catch (Exception e)
			{
				conn.Close();

				throw e;
			}
		}


		public IList<Forum> getForums()
		{
			List<Forum> forums = new List<Forum>();

			try
			{
				SqlConnection conn = new SqlConnection(connStr);
				SqlCommand cmd = new SqlCommand();
				cmd.Connection = conn;
				cmd.CommandText = "GetSubforums";
				cmd.CommandType = CommandType.StoredProcedure;

				conn.Open();

				SqlDataReader dr = cmd.ExecuteReader();



				while (dr.Read())
				{
					Forum f = new Forum();

					f.ID = Int64.Parse(dr["id"].ToString());
					f.Name = dr["name"].ToString();
					f.LastPostOn = dr["lastpost"].ToString();
					f.Description = dr["Description"].ToString();

					if (dr["postcount"].ToString() != "")
					{
						f.Postcount = Int64.Parse(dr["postcount"].ToString());
					}

					if (dr["topiccount"].ToString() != "")
					{
						f.Topiccount = Int64.Parse(dr["topiccount"].ToString());
					}

					forums.Add(f);
				}

				conn.Close();

			}
			catch (SqlException ex)
			{
				if (ex.ErrorCode == -2146232060)
				{
					//Know bug where SQL Server has closed the connection, but the connection is still available in the asp.net connection pool
					getForums();
					//return null;
				}
				else
				{
					throw ex;
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}


			return forums;
		}




	}
}